<?php

namespace App\Models;

use App\Models\Constant\Errors;
use App\Models\Constant\Fields;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class CommonModel extends Model implements Fields
{
    use SoftDeletes;
    static $SYSTEM = 'App\Models\SystemManagement';
    static $PersonInfo = 'App\Models\PersonInfoManagement';

    protected $guarded = ['is_admin','administrator'];

    /**
     * @param $model
     * @param $per
     * @return mixed
     * 获取表数据 带分页列表
     */
    public static function getTableData($model,$per){
        $query = $model::orderBy(Fields::ID)->paginate($per);
        return $query;
    }

    /**
     * @param $model
     * @return mixed
     * 获取表所有数据 不带分页
     */
    public static function getAllList($model){
        $query = $model::get();
        return $query;
    }

    /**
     * @param $model
     * @return mixed
     * 获取表数据
     * 转换成数组返回
     */
    public static function getTableDataDataToArr($model){
        $query = $model::orderBy(Fields::ID)
            ->get(['id','name','parent_id']);
        return $query;
    }

    /**
     * @param $con
     * @param $model
     * @return mixed
     * 通过条件查找
     */
    public static function getTableDataByCondition($con,$model){
        $query = $model::where(Fields::Name,'like','%'.$con->name.'%')->get();
        return $query;
    }

    /**
     * @param $arr
     * @param $id
     * @param $model
     * @return mixed
     * 修改某行表数据
     */
    public static function updateTableData($arr,$id,$model){
        $modelData = $model::find($id);
        $query = $modelData->update($arr);
        return $query;
    }

    /**
     * @param $ids
     * @param $model
     * @return Error
     * 批量删除表数据 软删除
     */
    public static function batchDelTableData($ids,$model){
        $query = $model::whereIn(Fields::ID,$ids)->delete();
        return $query;
    }

    /**
     * @param $ids
     * @param $model
     * @return Error
     * 批量恢复数据
     */
    public static function batchResetTableData($ids,$model){
        $query = $model::withTrashed()
            ->whereIn(Fields::ID,$ids)
            ->restore();
        return $query;
    }
}
